﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GpCommon.Models
{
    /// <summary>
    /// 分时数据类
    /// </summary>
    public class GpDayMSEntity
    {
        public string Code { get; set; }
        public DateTime CurTime { get; set; }
        /// <summary>
        /// 时间字节流记录
        /// </summary>
        public byte[] CurTimeByteBuffer { get; set; }
        public float StartPrice { get; set; }
        public float HighPrice { get; set; }
        public float LowPrice { get; set; }
        public float EndPrice { get; set; }
        public float TurnVolume { get; set; }
        public int Volume { get; set; }

        //均线值  计算所得
        public float MidPrice { get; set; }
        public static GpDayMSEntity Create(string code, byte[] bytes)
        {
            if (bytes.Length != 32) { return null; }
            GpDayMSEntity ent = null;
            try
            {
                int timeInt = BitConverter.ToInt32(new byte[] { bytes[0], bytes[1], 0, 0 }, 0);
                int year = (int)(Math.Floor(timeInt / 2048.0) + 2004);
                int month = (int)Math.Floor(timeInt % 2048 / 100.0);
                int day = timeInt % 2048 % 100;

                int minuteInt = BitConverter.ToInt32(new byte[] { bytes[2], bytes[3], 0, 0 }, 0);
                int hour = minuteInt / 60;
                int minute = minuteInt % 60;
                if (month == 7 && day == 7)
                {

                }
                float prePrice = BitConverter.ToSingle(new byte[] { bytes[4], bytes[5], bytes[6], bytes[7] }, 0);
                float highPrice = BitConverter.ToSingle(new byte[] { bytes[8], bytes[9], bytes[10], bytes[11] }, 0);
                float lowPrice = BitConverter.ToSingle(new byte[] { bytes[12], bytes[13], bytes[14], bytes[15] }, 0);
                float clsPrice = BitConverter.ToSingle(new byte[] { bytes[16], bytes[17], bytes[18], bytes[19] }, 0);
                float turnVolume = BitConverter.ToSingle(new byte[] { bytes[20], bytes[21], bytes[22], bytes[23] }, 0);
                int volume = BitConverter.ToInt32(new byte[] { bytes[24], bytes[25], bytes[26], bytes[27] }, 0);

                ent = new GpDayMSEntity();
                ent.Code = code;
                ent.CurTime = new DateTime(year, month, day, hour, minute, 0);
                ent.CurTimeByteBuffer = new byte[] { bytes[0], bytes[1], bytes[2], bytes[3] };
                ent.StartPrice = prePrice;
                ent.HighPrice = highPrice;
                ent.LowPrice = lowPrice;
                ent.EndPrice = clsPrice;
                ent.TurnVolume = turnVolume;
                ent.Volume = volume;
            }
            catch (Exception ex)
            {
                return null;
            }

            return ent;
        }
    }

    public class GpDayMS_OLEntity
    {
        public DateTime Time { get; set; }
        public float Price { get; set; }
        public float MidPrice { get; set; }
        public int Volume { get; set; }
    }
    public static class GpDay5MSEntityUtility
    {
        public static byte[] GetBytes(this GpDayMSEntity ent)
        {
            List<byte> writeByte = new List<byte>();
            writeByte.AddRange(ent.CurTimeByteBuffer);
            writeByte.AddRange(BitConverter.GetBytes(ent.StartPrice));
            writeByte.AddRange(BitConverter.GetBytes(ent.HighPrice));
            writeByte.AddRange(BitConverter.GetBytes(ent.LowPrice));
            writeByte.AddRange(BitConverter.GetBytes(ent.EndPrice));
            writeByte.AddRange(BitConverter.GetBytes(ent.TurnVolume));
            writeByte.AddRange(BitConverter.GetBytes(ent.Volume));
            writeByte.AddRange(new byte[4]);
            return writeByte.ToArray();
        }
    }
}
